package com.nononsenseapps.feeder.imageloader

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.graphics.drawable.LayerDrawable
import android.widget.ImageView
import androidx.appcompat.content.res.AppCompatResources
import com.lxj.xpopup.util.SmartGlideImageLoader
import com.nononsenseapps.feeder.ui.R
import com.nononsenseapps.feeder.glide.GlideImageLoader
import com.nononsenseapps.feeder.ui.common.htmlview.converter.SizedDrawable
import com.nononsenseapps.feeder.util.PrefUtils

object ImageLoaderManager{
    val xPopupImageLoader by lazy { SmartGlideImageLoader() }
   /* { object:XPopupImageLoader{

         override fun loadSnapshot(uri: Any, snapshot: PhotoView, srcView: ImageView?) {
             TODO("Not yet implemented")
         }

         override fun loadImage(
             position: Int,
             uri: Any,
             popupView: ImageViewerPopupView,
             snapshot: PhotoView,
             progressBar: ProgressBar
         ): View {
             GlideImageLoader.displayImage(popupView.context, uri as String,snapshot)
             return snapshot
         }

         override fun getImageFile(context: Context, uri: Any): File? {
            try {
                return Glide.with(context).downloadOnly().load(uri).submit().get()
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return null
        }
    }
    }*/
}

@SuppressLint("CheckResult")
fun displayImage(context: Context, path: String?, imageView: ImageView){
    GlideImageLoader.glide(context, path,false)
            .centerCrop()
            .also {
                it.error(
                        when (PrefUtils.isNightMode(context)) {
                            true ->  R.drawable.placeholder_image_list_night_64dp
                            false ->  R.drawable.placeholder_image_list_day_64dp
                        }
                )
            }
            .into(imageView)
}

fun creageVideoThumb(context: Context, sizedDrawable: SizedDrawable): Drawable {
    return createLayerThumb(context, sizedDrawable, R.drawable.youtube_icon)
}

fun creageAudioThumb(context: Context, sizedDrawable: SizedDrawable): Drawable {
    return createLayerThumb(context, sizedDrawable, when (PrefUtils.isNightMode(context)) {
        true -> R.drawable.ic_play_circle_outline_white_24dp
        false -> R.drawable.ic_play_circle_outline_night_24dp
    })
}

fun creageIframeThumb(context: Context, sizedDrawable: SizedDrawable): Drawable {
    return createLayerThumb(context, sizedDrawable, when (PrefUtils.isNightMode(context)) {
        true -> R.drawable.ic_open_in_browser_night_24dp
        false -> R.drawable.ic_open_in_browser_white_24dp
    })
}

fun creageAnimatorAbleThumb(context: Context, sizedDrawable: SizedDrawable): Drawable {
    return createLayerThumb(context, sizedDrawable, R.drawable.youtube_icon)
}

fun createLayerThumb(context: Context, sizedDrawable: SizedDrawable, resourDrawId:Int): Drawable {
    val playIcon = AppCompatResources.getDrawable(context, resourDrawId)!!

    // 20% size, in middle
    var w2 = playIcon.intrinsicWidth
    var h2 = playIcon.intrinsicHeight

    val ratio = h2.toDouble() / w2.toDouble()

    // Start with width which is known
    val relSize = 0.2
    w2 = (relSize * sizedDrawable.width).toInt()
    val left = ((sizedDrawable.width - w2).toDouble() / 2.0).toInt()
    // Then height is simple
    h2 = (ratio * w2).toInt()
    val top = ((sizedDrawable.height - h2).toDouble() / 2.0).toInt()

    // Create layer drawable
    return LayerDrawable(arrayOf(sizedDrawable.drawable, playIcon)).apply {
        // Need to set bounds on outer drawable first as it seems to override
        // child bounds
        this.setBounds(0, 0, sizedDrawable.width, sizedDrawable.height)
        // Now set smaller bounds on youtube icon
        playIcon.setBounds(left, top, left + w2, top + h2)
    }
}